package com.sky.service.impl;

import com.fasterxml.jackson.databind.util.BeanUtil;
import com.github.pagehelper.Page;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    DishMapper dishMapper;

    @Autowired
    SetmealMapper setmealMapper;

    @Override
    public void add(Dish dish) {
        dishMapper.insert(dish);
    }

    @Override
    public void addFlavor(List<DishFlavor> flavors) {
        dishMapper.insertFlavor(flavors);
    }

    @Override
    public PageResult queryPage(DishPageQueryDTO dishPageQueryDTO) {
        Page<DishVO> pageResult = dishMapper.queryPage(dishPageQueryDTO);
        return new PageResult(pageResult.getTotal(), pageResult.getResult());
    }

    @Override
    public Boolean hasStatus(List<Long> ids) {
        for (Long id: ids) {
            Dish dish = dishMapper.getById(id);
            System.out.println(dish.getStatus());
            if (dish.getStatus().equals(1)) return true;
        }
        return false;
    }

    @Override
    public Boolean hasSetMeal(List<Long> ids) {
        for (Long id: ids) {
            List<SetmealDish> list = setmealMapper.getByDishId(id);
            if (!list.isEmpty()) return true;
        }
        return false;
    }

    @Override
    public boolean isOwnSetMeal(Long id) {
        List<SetmealDish> list = setmealMapper.getByDishIdAndStatus(id, 1);
        return !list.isEmpty();
    }

    @Override
    public void setStatus(Dish dish) {
        dishMapper.update(dish);
    }

    @Override
    public DishVO getByIdForDishVO(Long id) {
        DishVO dishVO = new DishVO();
        // 查询dish
        Dish dish = dishMapper.getById(id);
        BeanUtils.copyProperties(dish, dishVO);

        // 查询口味
        List<DishFlavor> flavors = dishMapper.getByIdForFlavor(id);
        dishVO.setFlavors(flavors);

        return dishVO;
    }

    @Override
    public void deleteDish(List<Long> ids) {
        dishMapper.deleteDish(ids);
    }

    @Override
    public void deleteFlavor(List<Long> ids) {
        dishMapper.deleteFlavor(ids);
    }

    @Override
    @Transactional
    public void updateDishAndFlavor(DishVO dishVO) {
        // 更新dish
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishVO, dish);
        dishMapper.update(dish);

        // 删除口味dish_flavor
        ArrayList<Long> ids = new ArrayList<>();
        ids.add(dishVO.getId());
        dishMapper.deleteFlavor(ids);

        // 插入口味dish_flavor
        List<DishFlavor> flavors = dishVO.getFlavors();
        if(!flavors.isEmpty()) {
            flavors.forEach(item -> {
                item.setDishId(dishVO.getId());
            });
            dishMapper.insertFlavor(flavors);
        }
    }

    @Override
    public List<Dish> getByCategoryId(Integer categoryId) {
        return dishMapper.getByCategoryId(categoryId);
    }

    public List<DishVO> listWithFlavor(Dish dish) {

        // 某一类的已起售的菜品集合
        List<Dish> dishList = dishMapper.getByCategoryIdAndStatus(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            // 根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishMapper.getByIdForFlavor(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }
        return dishVOList;
    }
}
